<?php

/*
 * sqlite functions to create and alter new patch panels
 */
require_once('config.inc.php');


define("QUERY_SELECT_TABLE_PATCHPANEL", "SELECT name FROM sqlite_master WHERE type='table' and name='patchPanel'");
define("QUERY_CREATE_TABLE_PATCHPANEL", "CREATE TABLE patchPanel (id INTEGER PRIMARY KEY, floor int(1), rack char(1),patchPanel char(1),sockets int(2))");
define("QUERY_INSERT_SOCKETS", "INSERT INTO patchPanel VALUES(null,?,?,?,?)");
//define("QUERY_SOCKETS_SELECTION","SELECT * FROM patchPanel")

class PatchPanelDB {
    private $connection = null;    
    
    public function __construct(){
        //initilize connection
        $this->connection = new PDO('sqlite:php/networkmap.db');

        $stmt = $this->connection->prepare(QUERY_SELECT_TABLE_PATCHPANEL);
        $stmt->execute();
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        if(!$row){
            $this->init();
        }
        $stmt->closeCursor();
    }
    
    
    public function init(){
        
        $stmt_create = $this->connection->prepare(QUERY_CREATE_TABLE_PATCHPANEL);
        $stmt_create->execute();
        $stmt_create->closeCursor();
    }
    
    public function disconnect(){
	//$this->connection->close();
    }
    

    
    public function insertPatchPanel($f,$r,$pp,$s) {
        $stmt = $this->connection->prepare(QUERY_INSERT_SOCKETS);
        $stmt->bindParam('1', $f, PDO::PARAM_INT,1);
        $stmt->bindParam('2', $r, PDO::PARAM_STR,1);
        $stmt->bindParam('3', $pp, PDO::PARAM_STR,1);
        $stmt->bindParam('4', $s, PDO::PARAM_INT,2);
        $inserted = $stmt->execute();
         if(!$inserted){
            echo "unable to insert";
        }
    }
    
    public function socketsSelection($f,$r,$pp){
    
        
    }
}

